package com.yanqu.road.dao.impl.activity.drawPictrues;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.drawPictures.UserPictures;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DrawPicturesUserPicturesDataDaoImpl extends TempDataObjectDao<UserPictures> {
    @Override
    protected boolean addData(UserPictures data) {
        String sql = "insert into t_u_activity_picturesdata(activityId,userId,picturesId,num) values(?,?,?,?)";
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPicturesId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getNum()));
        if (getDb().executeNoneQuery(sql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected boolean updateData(UserPictures data) {
        String sql = "update t_u_activity_picturesdata set num=? where activityId=? and userId=? and picturesId=?";
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getNum()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPicturesId()));
        if (getDb().executeNoneQuery(sql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserPictures parseData(ResultSet rs) throws SQLException {
        UserPictures userPictures = new UserPictures();
        userPictures.setActivityId(rs.getInt("activityId"));
        userPictures.setUserId(rs.getLong("userId"));
        userPictures.setPicturesId(rs.getInt("picturesId"));
        userPictures.setNum(rs.getInt("num"));
        return userPictures;
    }

    public Map<Integer, Map<Integer, UserPictures>> getDrawPicturesUserPicturesData(List<Integer> activityIdList, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, UserPictures>> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_u_activity_picturesdata where userId = ? and activityId in(" + StringUtils.listToString(activityIdList, ",") + ")";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserPictures data = parse(rs);
                if (!dataMap.containsKey(data.getActivityId())) {
                    dataMap.put(data.getActivityId(), new ConcurrentHashMap<>());
                }
                dataMap.get(data.getActivityId()).put(data.getPicturesId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }
}
